package com.iocollege.tinyframe.mapper.system;

import com.iocollege.tinyframe.entity.system.SysRole;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 角色信息表 Mapper 接口
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2021-03-22
 */
public interface SysRoleMapper extends BaseMapper<SysRole> {

    @Select("select * from sys_role where id in(select id from sys_user_role where user_id=#{userId})")
    List<SysRole> selectRolesByUserId(Long userId);

    @Select("select * from sys_role r where r.role_name=#{roleName} limit 1")
    SysRole checkRoleNameUnique(String roleName);

    @Select("select * from sys_role r where r.role_key=#{roleKey} limit 1")
    SysRole checkRoleKeyUnique(String roleKey);

    @Select(" select count(1) from sys_user_role where id=#{roleId}  ")
    int countUserRoleByRoleId(Long roleId);


    /**
     * 根据用户名查询角色
     *
     * @param userName 用户名
     * @return 角色列表
     */
    @Select("select distinct r.id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.menu_check_strictly, r.dept_check_strictly,\n" +
            "            r.status, r.del_flag, r.create_time, r.remark \n" +
            "        from sys_role r\n" +
            "\t        left join sys_user_role ur on ur.role_id = r.id\n" +
            "\t        left join sys_user u on u.id = ur.user_id\n" +
            "\t        left join sys_dept d on u.dept_id = d.id\n" +
            "WHERE r.del_flag = '0' and u.user_name = #{userName}")
    public List<SysRole> selectRolesByUserName(String userName);



    @Select("select r.id\n" +
            "        from sys_role r\n" +
            "\t        left join sys_user_role ur on ur.role_id = r.id\n" +
            "\t        left join sys_user u on u.id = ur.user_id\n" +
            "\t    where u.id = #{userId}")
    List<Integer> selectRoleListByUserId(Long userId);
}
